/**
 * 
 */
package com.feib.stms.service.impl;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.stms.dao.MenuItemDao;
import com.feib.stms.dao.RoleDao;
import com.feib.stms.model.MenuItem;
import com.feib.stms.model.Role;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.MenuItemService;

/**
 * @author Jimmy Liu
 *
 */
@Service("menuItemService")
public class MenuItemServiceImpl extends BaseServiceImpl implements MenuItemService 
{

	private static final long serialVersionUID = -5793903975494699359L;

	@Resource(name="menuItemDao")
    private MenuItemDao menuItemDao;

    @Resource(name="roleDao")
	private RoleDao roleDao;
	
	public List<MenuItem> getAllEnabledMenuItemsWithRoles()
	{
		List<MenuItem> menuItems = menuItemDao.getAllEnabledMenuItems();
		
		for(MenuItem menuItem : menuItems)
		{
			List<Role> dbRoles = roleDao.getByMenuItemPk(menuItem);
			Set<Role> roles = new LinkedHashSet<Role>(dbRoles.size());
			roles.addAll(dbRoles);
			menuItem.setRoles(roles);
		}
		
		return menuItems;
	}
	
	public List<MenuItem> getAllEnabledMenuItemsForSecuritiesWithRoles(){
		List<MenuItem> menuItems = menuItemDao.getAllEnabledMenuItemsForSecurities();
		
		for(MenuItem menuItem : menuItems)
		{
			List<Role> dbRoles = roleDao.getByMenuItemPk(menuItem);
			Set<Role> roles = new LinkedHashSet<Role>(dbRoles.size());
			roles.addAll(dbRoles);
			menuItem.setRoles(roles);
		}
		
		return menuItems;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#findPaginatedListByMenuItem(java.lang.String, java.lang.String, java.lang.String)
	 */
	public PaginatedList<MenuItem> findForPaginatedListByMenuItem(MenuItem menuItem ,String menuType, PaginatedList paginatedList) {
		return menuItemDao.queryForPaginatedListByMenuItem(menuItem, menuType, paginatedList);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#initMenuItemOptionList()
	 */
	public List<MenuItem> initMenuItemOptionList() {
		return menuItemDao.initMenuItemOptionList();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#createMenuItem(com.feib.stms.model.MenuItem)
	 */
	public int saveMenuItem(MenuItem menuItem) {
		return menuItemDao.insert(menuItem);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#getByMenuItemPk(java.lang.Long)
	 */
	public MenuItem getByMenuItemPk(Long menuItemPk) {
		return menuItemDao.getByMenuItemPk(menuItemPk);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#deleteMenuItem(java.lang.Long)
	 */
	public int deleteMenuItem(Long pk) {
		return menuItemDao.delete(pk);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#updateMenuItem(com.feib.stms.model.MenuItem)
	 */
	public int updateMenuItem(MenuItem menuItem) {
		//去資料庫查詢該筆MenuItem
		MenuItem dbMenuItem = menuItemDao.getByMenuItemPk(menuItem.getMenuItemPk());
		if(menuItem != null){
			dbMenuItem.setParentMenuItem(menuItem.getParentMenuItem());
			dbMenuItem.setMenuNo(menuItem.getMenuNo());
			dbMenuItem.setActionName(menuItem.getActionName());
			dbMenuItem.setActionPage(menuItem.getActionPage());
			dbMenuItem.setActionBeanClass(menuItem.getActionBeanClass());
			dbMenuItem.setOrderNo(menuItem.getOrderNo());
			dbMenuItem.setEnabled(menuItem.getEnabled());
			dbMenuItem.setSecurities(menuItem.getSecurities());
		}
		return menuItemDao.update(dbMenuItem);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.MenuItemService#countParentMenuItem(java.lang.Long)
	 */
	public int countParentMenuItem(Long pk) {
		return menuItemDao.countParentMenuItem(pk);
	}
}
